package com.iexbuy.ihg.ui.fragment;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.iexbuy.ihg.R;
import com.iexbuy.ihg.common.CommUtil;
import com.iexbuy.ihg.entity.AddressObject;
import com.iexbuy.ihg.entity.ShopItemDetailObject;
import com.iexbuy.ihg.entity.XiaoFeiOrderConfirmObject;
import com.iexbuy.ihg.entity.YunItemListObjectResponse;
import com.iexbuy.ihg.net.ServiceHelper;
import com.iexbuy.ihg.utils.JsonUtils;
import com.iexbuy.ihg.utils.ToastUtils;
import com.jingchen.pulltorefresh.PullToRefreshLayout;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import java.util.HashMap;

public class HuanGouOrderPayFragment extends BaseFragment {
	
	private static HuanGouOrderPayFragment homeFragmentInstent;
	private View rootView;

	@ViewInject(R.id.msgListLayout)
	private PullToRefreshLayout msgListRefreshLayout;
	@ViewInject(R.id.listView)
	private ListView listView;

	private LinearLayout listHeaderView;
	@ViewInject(R.id.productName)
	private TextView productName;
	@ViewInject(R.id.price)
	private TextView price;
	@ViewInject(R.id.addressContacter)
	private TextView addressContacter;
	@ViewInject(R.id.addressMobile)
	private TextView addressMobile;
	@ViewInject(R.id.address)
	private TextView address;

	private static ShopItemDetailObject shopItemDetailObject;
	private static XiaoFeiOrderConfirmObject xiaoFeiOrderConfirmObject;
	private static AddressObject selectedAddress;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		homeFragmentInstent = this;
	}

	@Override
	protected View _onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.fragment_huan_gou_order_pay, container, false);
		ViewUtils.inject(this, rootView);
		initView(rootView);
		return rootView;
	}

	private void initView(View rootView) {
		mBaseActivity.setTitle(getResources().getString(R.string.title_huan_gou_order_pay));
		mBaseActivity.setTitleLayoutVisibility(true);
		mBaseActivity.setHomeActivity(false);

		initOrderlView();
	}

	private void initOrderlView() {
		if (shopItemDetailObject == null) return;
		productName.setText(shopItemDetailObject.getItemName());
		price.setText(shopItemDetailObject.getNewPrice());

		if (selectedAddress != null) {
			addressContacter.setText(selectedAddress.getReceiver());
			addressMobile.setText(selectedAddress.getMobile());
			address.setText(selectedAddress.getAddress());
		}
	}

	/**
	 * 提交订单
	 * @param view
	 */
	@OnClick(R.id.submitOrderBtn)
	public void onSubmitOrderBtnClick(View view) {
		CommUtil.showTwoSelectAlert(mBaseActivity, "用我的金币进行换购支付？", "确定", "取消", new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				submitOrder();
			}
		}, null);
	}

	private void submitOrder() {
		HashMap<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("goodsId", shopItemDetailObject.getItemId());
		paramMap.put("requestNumber", "1");
		paramMap.put("phone", "");
		if (selectedAddress != null) {
			paramMap.put("addrId", selectedAddress.getAddressId());
		}
		paramMap.put("couponId", "");
		ServiceHelper.send(mBaseActivity, ServiceHelper.SUBMIT_DEAL, paramMap,
				new RequestCallBack<String>() {
					@Override
					public void onStart() {
						mBaseActivity.showDialog("提交中...");
					}

					@Override
					public void onCancelled() {
						mBaseActivity.closeDialog();
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						mBaseActivity.closeDialog();
						xiaoFeiOrderConfirmObject = JsonUtils.parseJsonStrToObject(JsonUtils.getJsonSubStringByKey(responseInfo.result, "body"), XiaoFeiOrderConfirmObject.class);
						submitGoldCoinPay();
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						mBaseActivity.closeDialog();
						ToastUtils.toast(mBaseActivity, msg);
					}
				});
	}

	private void submitGoldCoinPay() {
		HashMap<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("orderId", xiaoFeiOrderConfirmObject.getId());
		ServiceHelper.send(mBaseActivity, ServiceHelper.SUBMIT_GOLD_PAYMENT, paramMap,
				new RequestCallBack<String>() {
					@Override
					public void onStart() {
						mBaseActivity.showDialog("提交中...");
					}

					@Override
					public void onCancelled() {
						mBaseActivity.closeDialog();
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						mBaseActivity.closeDialog();
						CommUtil.showAlert(mBaseActivity, "兑换成功", "您可在“我的订单”界面查看本次兑换的物品", new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialogInterface, int i) {
								mBaseActivity.finish();
							}
						});
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						mBaseActivity.closeDialog();
						ToastUtils.toast(mBaseActivity, msg);
					}
				});
	}


	public static void setShopItemDetailObject(ShopItemDetailObject shopItemDetailObject) {
		HuanGouOrderPayFragment.shopItemDetailObject = shopItemDetailObject;
	}

	public static void setXiaoFeiOrderConfirmObject(XiaoFeiOrderConfirmObject xiaoFeiOrderConfirmObject) {
		HuanGouOrderPayFragment.xiaoFeiOrderConfirmObject = xiaoFeiOrderConfirmObject;
	}

	public static void setSelectedAddress(AddressObject selectedAddress) {
		HuanGouOrderPayFragment.selectedAddress = selectedAddress;
	}

	/**
	 * @return the homeFragmentInstent
	 */
	public static HuanGouOrderPayFragment getHomeFragmentInstent() {
		return homeFragmentInstent;
	}

}
